home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / database / hl / hl-1.002 / hl-1 / Include / common.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-14  |  7.3 KB  |  257 lines

  1. /* -*- Mode: C -*- */
  2. /* common.h - common include file - all other include files (and
  3.  *          programs) include this file
  4.  * Created by Robert Heller on Fri Dec  6 19:42:59 1991
  5.  *
  6.  * ------------------------------------------------------------------
  7.  * Home Libarian by Deepwoods Software
  8.  * Common Header Files
  9.  * ------------------------------------------------------------------
  10.  * Modification History:
  11.  * ------------------------------------------------------------------
  12.  * Contents:
  13.  * ------------------------------------------------------------------
  14.  * 
  15.  * 
  16.  * Copyright (c) 1991,1992 by Robert heller (D/B/A Deepwoods Software)
  17.  *        All Rights Reserved
  18.  * 
  19.  */
  20.  
  21. #ifndef _COMMON_
  22. #define _COMMON_
  23. #include <stdlib.h>        // standard C library functions
  24. #include <string.h>
  25. #include <errno.h>
  26. /*
  27.  * O/S dependent stuff:
  28.  *    1) we need file modes (passed as args to open and creat (create under
  29.  *     OSK)
  30.  *    2) we need terminal mode hacking
  31.  *    3) we need signal and trap interception stuff
  32.  */
  33. #ifdef OSK
  34. #include <modes.h>        // file modes
  35. // generic symbols (hide O/S dependent names)
  36. #define ReadMode S_IREAD
  37. #define ReadWriteMode S_IREAD|S_IWRITE
  38. #define ReadFlags S_IREAD
  39. #define ReadWriteFlags S_IREAD|S_IWRITE
  40. extern "C" {
  41. extern char* strerror(int);    // missing from the OSK library...
  42. }
  43. // signal hackery
  44. #include "/dd/defs/signal.h"    // the G++ signal.h is a UNIX style signal.h
  45. // terminal modes
  46. //#include <sgstat.h>        // Get/Set tty modes (OSK)
  47. // special library functions
  48. //typedef int (*icept)(int);
  49. //extern "MWC" {
  50. //    int _ss_opt(int,char*);
  51. //    int _gs_opt(int,char*);
  52. //    int _gs_rdy(int);
  53. //    int intercept(icept);
  54. //    char *strerror(int);
  55. //};
  56. #else
  57. #include <fcntl.h>
  58. #include <sys/stat.h>
  59. // generic symbols (hide O/S dependent names)
  60. #define ReadMode S_IREAD
  61. #define ReadWriteMode S_IREAD|S_IWRITE
  62. #ifdef MESSYDOS
  63. #define ReadFlags O_RDONLY|O_BINARY
  64. #define ReadWriteFlags O_RDWR|O_BINARY
  65. #else
  66. #include <unistd.h>
  67. #define ReadFlags O_RDONLY
  68. #define ReadWriteFlags O_RDWR
  69. #endif
  70. #endif
  71. #ifdef unix
  72. #include <sys/ioctl.h>
  73. #endif
  74. // Global constants:
  75. // KeySize is how long key strings can be.  This includes the nul byte
  76. const KeySize = 36;
  77. // Tree order
  78. const Order   =  10;
  79. // Key type
  80. typedef char Key[KeySize];
  81. // with an order 10 tree and a 32 byte key string + standard overhead
  82. // (pointers, size info, data record pointers), we will get a tree node
  83. // (page) of just under 1024 bytes.  With a small filler, pages will
  84. // be 1024 bytes in size.  This is a reasonable size for most O/Ss.
  85. const SectorSize = 1024;
  86.  
  87. // Global enumerated types
  88. // Your basic true/false type:
  89. typedef int Boolean;
  90. const Boolean false = 0;
  91. const Boolean true  = 1;
  92. // File directions:
  93. enum Direction { in, out, inout };
  94. // File open status
  95. enum OpenStatus { failure, openold, opennew };
  96. // LastSearchType.  Used by SearchXX and SearchXXAgain.
  97. enum LSType { none, id, title, author, subj };
  98. // open modes
  99. enum OpenMode { ReadOnly = 0x001, ReadWrite = 0x003, ModeMask = 0x0FF,
  100.         Create = 0x100 };
  101. // types of cards
  102. enum CardType { Book = 'B', Magazine = 'M', CD = 'D', AudioCassette = 'C',
  103.         Album = 'A', LaserDisk = 'L', VHSVideo = 'V', BetaVideo = 'S',
  104.         EightMM = '8', EightTrack = 'E', DAT = '4', Other = 'O' };
  105. // Terminal command keys
  106. enum  CommandKeys {
  107.     escCmd    = -1,        // ESC key
  108.     upCmd   = -2,        // Up Arrow (^P)
  109.     downCmd = -3,        // Down Arrow (^N)
  110.     leftCmd = -4,        // Left Arrow (^B)
  111.     rightCmd = -5,        // Right Arrow (^F)
  112.     backCmd = -6,        // BackSpace (^H,RUBOUT)
  113.     deleCmd = -7        // DeleteLine (^U)
  114. };
  115. // Terminal drawing modes
  116. enum Mode {
  117.     defPen     = 0x00000,
  118.     graphPen   = 0x00100,
  119.     revsPen    = 0x00200,
  120.     charMask   = 0x000FF,
  121.     modeMask   = 0x0FF00
  122. };
  123. // Types of errors
  124. enum ErrKind {
  125.     memErr,  termErr, sysErr
  126. };
  127.  
  128. // Basic structure classes
  129.  
  130. // A disk record contains a size and a file offset.
  131. struct DiskRecord {
  132.     long int record_size;
  133.     long record_address;
  134.     DiskRecord (long int size = 0, long addr = 0L) 
  135.            {record_size = size; record_address = addr;}
  136. };
  137.  
  138. // A disk page is simular, except the size is fixed.
  139. // there are some special operator and constructor methods
  140. // used to make things convenient, since these are used like
  141. // pointers
  142. struct DiskPage {
  143.     const long int record_size = SectorSize;
  144.     long  record_address;
  145.     DiskPage (DiskPage& a) {record_address = a.record_address;}
  146.     DiskPage (long addr = 0L) {record_address = addr;}
  147.     inline friend Boolean operator == (DiskPage& a,DiskPage& b)
  148.                 {return(a.record_address == b.record_address);}
  149.     inline friend Boolean operator != (DiskPage& a,DiskPage& b)
  150.                 {return(a.record_address != b.record_address);}
  151.     inline DiskPage& operator = (DiskPage& a)
  152.                 {record_address = a.record_address;return *this;}
  153.  
  154. };
  155.  
  156. // A data item - has a key, a data record "pointer", and a child page
  157. // "pointer".
  158. struct Item {
  159.     Key key;
  160.     DiskRecord data;
  161.     DiskPage right;
  162. };
  163.  
  164. // A page is a tree node.
  165. // It has a size (number of used items), pointers to a child and parent,
  166. // a vector of items, and some filler (used to make sure the page is
  167. // exactly 1024 bytes in size).
  168. struct Page {
  169.     long int size;
  170.     DiskPage left;
  171.     DiskPage parent;
  172.     long int parentidx;
  173.     Item items[Order*2];
  174.     char filler[SectorSize-(sizeof(long int) +
  175.                 (2*sizeof(DiskPage)) +
  176.                 sizeof(long int) +
  177.                 (sizeof(Item)*Order*2))];
  178. };
  179.  
  180. // Page Table Entry
  181. // Has a dirty flag, a pointer to the disk page, and a pointer to
  182. // the core copy of the disk page.
  183. struct PTEntry {
  184.     Boolean isdirty;
  185.     DiskPage diskpage;
  186.     Page     *corepage;
  187. };
  188.  
  189. // HomeBlock - this is a 1024 byte record at the start of the file.
  190. // it contains a 8 byte magic header, pointers to the roots of the
  191. // four trees and a vector of available pre-allocated pages.
  192. const int MaxNumFree = (SectorSize - ( 8 + (4 * sizeof(DiskPage)) + 
  193.                        sizeof(long int))) /
  194.                sizeof(DiskPage);
  195.  
  196. struct HomeBlock {
  197.     const char  * const Magic = "LIBRV000";
  198.     char magic[8];
  199.     DiskPage IdRoot, TitleRoot, AuthorRoot, SubjRoot;
  200.     long int numfree;
  201.     DiskPage freepages[MaxNumFree];
  202. };
  203.  
  204. // Core resident record - it has a size and a chunk of memory (the
  205. // record itself).
  206. // Lots of fun.  I wish C++ had a garbage collector...
  207. struct Record {
  208.     long int size;
  209.     char *buffer;
  210.          Record () {size = 0; buffer = 0;}        // empty buffer
  211.          Record (long int size) {Record::size = size; // preallocated buffer
  212.                      buffer = new char[size];}
  213.          Record (Record& rec)        // record from another record
  214.         {
  215.             size = rec.size;
  216.             if (size > 0) {
  217.                 buffer = new char[size];
  218.                 memcpy(buffer,rec.buffer,size);
  219.             }
  220.         }
  221.         ~Record () {if (size > 0) delete buffer;}    // Oh for a GC!
  222.     Record& operator = (Record& rec)        // copy a record
  223.         {
  224.             if (size < rec.size) {
  225.                 delete buffer;
  226.                 buffer = new char[rec.size];
  227.             } else if (rec.size <= 0 && size > 0)
  228.                 delete buffer;
  229.             size = rec.size;
  230.             if (rec.size > 0)
  231.                 memcpy(buffer,rec.buffer,rec.size);
  232.             return *this;
  233.         }
  234.     void NewBuffer(long int size)        // buffer re-allocator
  235.         {
  236.             if (Record::size > 0) delete buffer;
  237.             Record::size = size;
  238.             if (size > 0) buffer = new char[size];
  239.         }            
  240. };
  241.  
  242. // In core item.  Code outside of vBTree.cc never does actual disk I/O.
  243. // Instead it passes in-core records and the insertion, searching, and
  244. // traversal code reads and writes the data from and to the disk file.
  245. struct CoreItem {
  246.     Key key;
  247.     Record data;
  248. };
  249.  
  250. // Function pointer types
  251. typedef void (*TravFunc)(CoreItem*,int);
  252. typedef void (*ErrFun)(int,const char*);
  253. typedef void (*InterruptFun)(void);
  254.  
  255. #endif
  256.  
  257.